package com.zhongketech.sunflower.view;

import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.filter.AppManager;
import com.zhongketech.sunflower.util.Constants;
import com.zhongketech.sunflower.util.Utilities;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class StartAppAdater extends BaseAdapter{

	private final String TAG = "StartAppAdater";

	private Context context = null;

	public final class ViewHolder{
		public ImageView itemicon;
		public CheckBox itemcheck_start;
		public TextView itemtext;
	}



	private LayoutInflater mInflater;

	public StartAppAdater(Context context){
		this.mInflater = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return AppManager.getInstance(context).getCount();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		ViewHolder holder = null;

		if(null == convertView)
		{
			holder = new ViewHolder(); 
			convertView = mInflater.inflate(R.layout.startappitem, null);
			holder.itemicon = (ImageView) convertView.findViewById(R.id.itemicon);
			holder.itemcheck_start = (CheckBox) convertView.findViewById(R.id.itemcheck_start);
			holder.itemcheck_start.setOnCheckedChangeListener(new CheckBoxListener());
			holder.itemtext = (TextView) convertView.findViewById(R.id.itemtext);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)convertView.getTag();
		}

		//Utilities.logCat(TAG, "position: "+position+" convertView: "+convertView+" parent:"+parent,Constants.LOG_INFO);
		Drawable drawable = AppManager.getInstance(context).getAppIcon(position);
		if(null != drawable)
		{
			holder.itemicon.setImageDrawable(drawable);
		}
		else
		{
			holder.itemicon.setImageResource(R.drawable.icon);
		}

		holder.itemtext.setText(AppManager.getInstance(context).getAppName(position));
		holder.itemcheck_start.setTag(AppManager.getInstance(context).getAppPackage(position));
		if(AppManager.getInstance(context).getAppInfo(position).isStop)
		{
			holder.itemcheck_start.setChecked(true);
		}
		else
		{
			holder.itemcheck_start.setChecked(false);
		}

		return convertView;
	}

	class CheckBoxListener implements OnCheckedChangeListener
	{

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if(isChecked){  
				//Toast  
				//Toast.makeText(context, buttonView.getTag()+" 被选择禁止启动",Toast.LENGTH_SHORT ).show();
				AppManager.getInstance(context).setAppInfoStopFlag( (String) buttonView.getTag(),true);
			}else{  
				AppManager.getInstance(context).setAppInfoStopFlag( (String) buttonView.getTag(),false);
			}  
		}

	}
}
